﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Quartz;

namespace Jsptpd.Common.quartzHelper
{
    public class JobHelper
    {
        private log4net.ILog logger = log4net.LogManager.GetLogger(typeof(Jsptpd.Common.quartzHelper.JobHelper));
        
        public void fireInSpecifyTime(IScheduler scheduler,string cronexpression)
        {
            try
            {
                IJobDetail job = JobBuilder.Create<Jsptpd.JobImpl.wfTaskNotify>()
                                         .WithIdentity("specifyTimeFireJob", "specifyFireJobGroup")
                                         .Build();
                ITrigger trigger = TriggerBuilder.Create()
                                 .WithIdentity("specifyTimeFireTrigger", "specifyTimeFireTriGroup")
                                 .WithSchedule(CronScheduleBuilder.CronSchedule(new CronExpression(cronexpression)))
                                 .Build();
                scheduler.ScheduleJob(job, trigger);
            }
            catch (Exception ex)
            {
                logger.Error("Inital job or trigger failed,the detail info:"+ex.ToString());                 
            }            
            scheduler.Start();
            logger.Info("Inital log and job success...");
        }

        public void fireInSeconds(IScheduler scheduler)
        {
            IJobDetail jobDetail = JobBuilder.Create<Jsptpd.JobImpl.wfTaskNotify>()
                .WithIdentity("myJob")
                .Build();            
            ITrigger trigger = TriggerBuilder.Create()
                .WithIdentity("myTrigger")
                // fire every seconds
                .WithSimpleSchedule(x => x.WithIntervalInSeconds(1).RepeatForever())
                // start on the next even minite
                .StartAt(DateBuilder.FutureDate(1, IntervalUnit.Second))
                .Build();            
            scheduler.ScheduleJob(jobDetail, trigger); 
            scheduler.Start();           
        }
    }
}
